package tokenutils

import (
	"administrator/app/model"
	"gitee.com/aurora-engine/aurora/utils/timeutils"
	"gitee.com/aurora-engine/web"
	"github.com/golang-jwt/jwt"
	"time"
)

type Token struct {
	jwt.StandardClaims
	ID int
}

func CreateToken(user model.UserModel) string {
	s := timeutils.DateTime()
	// 设置token10天过期
	parse, err := time.Parse("2006-01-02 15:04:05", time.Now().Add(time.Duration(10*24*60*60)*time.Second).Format("2006-01-02 03:04:05"))
	if err != nil {
		panic(err)
	}
	claims := jwt.NewWithClaims(jwt.SigningMethodHS512, Token{
		StandardClaims: jwt.StandardClaims{
			Issuer:    s,
			ExpiresAt: parse.Unix(),
		},
		ID: user.Id,
	})
	key := []byte("all")
	signingString, err := claims.SignedString(key)
	if err != nil {
		panic(err)
	}
	return signingString
}

// GetToken Token 初始化函数
func GetToken(ctx web.Context) any {
	t := ctx["token"]
	if t == nil {
		t = &Token{}
	}
	return t
}
